package fr.cephb.io;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;

/** various IO utilities for dumping a source of data to another */
public class IOUtils {

	/** copy an InputStream to a String */
	public static String copyToString(InputStream in) throws IOException
		{
		return copyToString(new InputStreamReader(in));
		}
	
	/** copy a Reader to a String */
	public static String copyToString(Reader in) throws IOException
		{
		StringWriter b= new StringWriter();
		copyTo(in,b);
		return b.toString();
		}
	
	/** copy a Reader to a Writer */
	public static void copyTo(Reader in,Writer out) throws IOException
		{
		char buf[]= new char[2048];
		int n;
		while((n=in.read(buf))!=-1)
			{
			out.write(buf, 0, n);
			}
		out.flush();
		}
	
	/** copy an InputStream to an OutputStream */
	public static void copyTo(InputStream in,OutputStream out) throws IOException
		{
		byte buf[]= new byte[2048];
		int n;
		while((n=in.read(buf))!=-1)
			{
			out.write(buf, 0, n);
			}
		out.flush();
		}
}
